﻿using System;
using System.Collections.Generic;
using System.Numerics;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Me.Prettyprint.Cassandra;
using Me.Prettyprint.Cassandra.Connection;
using Me.Prettyprint.Cassandra.Model;
using Me.Prettyprint.Cassandra.Service;
using Me.Prettyprint.Cassandra.Serializers;
using Me.Prettyprint.Hector.Api;
using Me.Prettyprint.Hector.Api.Beans;
using Me.Prettyprint.Hector.Api.Ddl;
using Me.Prettyprint.Hector.Api.Factory;
using Me.Prettyprint.Hector.Api.Mutation;
using Me.Prettyprint.Hector.Api.Query;
using Org.Apache.Cassandra.Thrift;
using Thrift;

namespace UnitTest
{
    [TestClass]
    public class QuickStart
    {
        static Cluster cluster = null;
        static CassandraHostConfigurator config = new CassandraHostConfigurator();
        const string TEST_KEYSPACE1 = "mykeyspace";
        public static List<string> keys = new List<string>();
        public static Random random = new Random();

        public QuickStart()
        {
            Me.Prettyprint.Cassandra.Utils.Diagnostics.CassandraTraceSwitch.Level = System.Diagnostics.TraceLevel.Info;
            Me.Prettyprint.Cassandra.Utils.ConfigureLogging.EnableConsoleTraceListener();
        }
        Cluster RunningCluster
        {
            get
            {
                /// Note: Change the host IP to the address Cassandra is using
                config.Hosts = "192.168.1.43";
                if (cluster == null)
                {
                    cluster = HFactory.GetOrCreateCluster("test cluster", config);
                }
                return cluster;
            }
            set
            {
                cluster = value;
            }
        }
        [TestMethod]
        public void SaveMyData()
        {
            string columnFamilyName = "MyColumnFamily";
            if (RunningCluster == null)
            {
                Assert.Fail();
            }
            else
            {
                KeyspaceDefinition ksdef = null;
                try
                {
                    ksdef = RunningCluster.DescribeKeyspace(TEST_KEYSPACE1);
                }
                catch (Exception e)
                {
                }
                if (ksdef == null)
                {
                    ksdef = HFactory.CreateKeyspaceDefinition(TEST_KEYSPACE1);
                    ksdef.ReplicationFactor = 1;
                    Assert.IsNotNull(ksdef);
                    RunningCluster.AddKeyspace(ksdef);
                }
                // add some ColumnFamilies
                ColumnFamilyDefinition cfdef = HFactory.CreateColumnFamilyDefinition(TEST_KEYSPACE1, columnFamilyName);
                cfdef.ReplicateOnWrite = true;
                cfdef.DefaultValidationClass = ComparatorType.UTF8TYPE.ClassName;
                cfdef.ComparatorType = ComparatorType.UTF8TYPE;
                cfdef.KeyValidationClass = ComparatorType.UTF8TYPE.ClassName;
                cfdef.ColumnType = ColumnType.Standard;
                for (int i = 1; i < 6; i++)
                {
                    BasicColumnDefinition columnDef = new BasicColumnDefinition();
                    string colname = "column" + i.ToString();
                    columnDef.Name = StringSerializer.Instance.ToBytes(colname);
                    columnDef.ValidationClass = ComparatorType.UTF8TYPE.ClassName;
                    columnDef.IndexName = colname + "index";
                    columnDef.IndexType = ColumnIndexType.KEYS;
                    cfdef.ColumnMetadata.Add(columnDef);
                }
                RunningCluster.AddColumnFamily(cfdef, true);
                Keyspace kspace = HFactory.CreateKeyspace(TEST_KEYSPACE1, RunningCluster);

                // now we try to save some data
                int records = 2;
                for (int i = 0; i < records; i++)
                {
                    Mutator<string> stringMutator = HFactory.CreateMutator<string>(kspace, StringSerializer.Instance);
                    Guid newguid = Guid.NewGuid();
                    keys.Add(newguid.ToString());
                    Console.WriteLine(" --- " + newguid.ToString() + " --- ");
                    for (int c = 1; c < 6; c++)
                    {
                        string colname = "column" + c.ToString();
                        string colval = "value" + (c * 42);
                        HColumn<string, string> column = HFactory.CreateColumn(colname, colval);
                        stringMutator.AddInsertion(newguid.ToString(), columnFamilyName, column);
                    }
                    stringMutator.Execute();
                }
                CqlQuery<string,string,string> query = new CqlQuery<string,string,string>(kspace, StringSerializer.Instance,
                    StringSerializer.Instance,StringSerializer.Instance);
                query.SetQuery("select * from " + columnFamilyName);
                QueryResult<CqlRows<string,string,string>> result = query.Execute();
                CqlRows<string,string,string> rows = result.Get();
                foreach (Row<string,string,string> row in rows.List)
                {
                    Console.WriteLine(row.Key);
                    ColumnSlice<string,string> cslice = row.ColumnSlice;
                    List<HColumn<string,string>> columns = cslice.GetColumns();
                    foreach (HColumn<string, string> c in columns)
                    {
                        Console.WriteLine("    " + c.Name + "=" + c.Value);
                    }
                    Console.WriteLine("");
                }
            }
        }
    }
}
